package utils.check;

import models.User;
import net.sf.oval.Validator;
import net.sf.oval.configuration.annotation.AbstractAnnotationCheck;
import net.sf.oval.context.OValContext;
import net.sf.oval.exception.OValException;
import controllers.Security;

public class ConnectedUserCheck extends AbstractAnnotationCheck<ConnectedUserAnno> {

	/** Error key message */
	public final static String MESSAGE_KEY = "validation.connecteduser";
	
	@Override
	public void configure(final ConnectedUserAnno cUser) {
		setMessage(cUser.message());
	}
	
	@Override
	public boolean isSatisfied(final Object validatedObj, final Object value, final OValContext context,
			final Validator validator) throws OValException {
		
		boolean bResult = false;
		try {
			User user = (User) value;
			Long connectedUserId = Security.getConnectedUser().getId();
			if (user != null && user.getId().equals(connectedUserId)) {
				bResult = true;
			}	
		} catch (Throwable e) {
			// do nothing
		}

		return bResult;
	}

	
	
}
